import routeTable from '@/router/routeTable'
import Index from '@/views/home/index.vue'

const state = {
  filterRoute: []
}

const mutations = {
  /* 添加路由表 */
  addRouteTable (state, filterRoute) {
    state.filterRoute = [
      {
        path: '/index',
        name: 'Index',
        redirect: '/home',
        component: Index,
        meta: {
          title: '首页'
        },
        children: filterRoute
      }
    ]
  },
  /* 移除路由表 */
  removeRouteTable (state) {
    state.filterRoute = []
  }
}

const actions = {
  // 获取菜单权限表
  getMenuKeys ({ commit }, menuKeys) {
    return new Promise(resolve => {
      // 默认显示首页
      menuKeys.push(':home')
      // 过滤路由
      const filterRoute = routeTable.filter(item => item.meta).filter(item => menuKeys.includes(item.meta.id))
      // 添加404路由
      filterRoute.push({ path: '/:pathMatch(.*)*', component: () => import('@/components/404/index.vue') })
      // 添加路由
      commit('addRouteTable', filterRoute)
      resolve()
    })
  }
}

const getters = {
  /* 获取路由表 */
  getRouteTable (state) {
    return state.filterRoute
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
